package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.enums.LoginDevice;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.ViewParam;
import com.cloud.user_info.entity.SysProject;
import com.cloud.user_info.service.ISysProjectService;
import com.cloud.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统项目管理
 *
 * @author yzj
 * @since 2022-07-25
 */
@Tag(name = "系统项目管理")
@RestController
@RequestMapping("/system/project")
public class SysProjectController extends BaseController {

    @Autowired
    private ISysProjectService sysProjectService;

    @Operation(summary = "查询页面配置信息")
    @GetMapping("/inAuth/getViewInfo")
    public Result getViewInfo(ViewParam viewParam) {
        if (StringUtils.isEmpty(viewParam.getLoginType())) {
            viewParam.setLoginType(LoginDevice.PC.getCode());
        }
        if (StringUtils.isEmpty(viewParam.getSysCode())) {
            return Result.error("系统编号不能为空");
        }
        return sysProjectService.getViewInfo(viewParam);
    }

    @Operation(summary = "系统项目分页")
    @RequestPermissions("system:project:list")
    @GetMapping("/list")
    public Result list(SysProject sysProject) {
        startPage();
        List<SysProject> list = sysProjectService.selectSysViewList(sysProject);
        return getDataTable(list);
    }

    @Operation(summary = "系统项目列表")
    @GetMapping("/allList")
    public Result allList(SysProject sysProject) {
        return Result.success("查询成功", sysProjectService.selectSysViewList(sysProject));
    }

    @Operation(summary = "添加系统项目信息")
    @Logs(title = "添加系统页面", businessType = BusinessType.INSERT)
    @RequestPermissions("system:project:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysProject sysProject) {
        return toResult(sysProjectService.insertObject(sysProject));
    }

    @Operation(summary = "修改系统项目信息")
    @Logs(title = "修改系统页面", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:project:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysProject sysProject) {
        return toResult(sysProjectService.updateObject(sysProject));
    }

    @Operation(summary = "查询系统项目详情")
    @RequestPermissions("system:project:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", sysProjectService.getObjectInfo(id));
    }

    @Operation(summary = "删除系统项目信息")
    @Logs(title = "删除系统页面", businessType = BusinessType.DELETE)
    @RequestPermissions("system:project:remove")
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(sysProjectService.removeByIds(list));
    }

    @Operation(summary = "开启配置应用状态")
    @Logs(title = "开启配置应用状态", businessType = BusinessType.UPDATE)
    @PostMapping("/turnOnStatus")
    public Result turnOnStatus(Long id) {
        return toResult(sysProjectService.turnOnStatus(id));
    }

    @Operation(summary = "关闭配置应用状态")
    @Logs(title = "关闭配置应用状态", businessType = BusinessType.UPDATE)
    @PostMapping("/closureStatus")
    public Result closureStatus(Long id) {
        return toResult(sysProjectService.closureStatus(id));
    }

//    @Operation(summary = "批量添加用户授权项目角色")
//    @Parameters({
//            @Parameter(name = "sysCode", description = "项目编号"),
//            @Parameter(name = "roleIds", description = "角色ID[多个用,隔开]")
//    })
//    @Logs(title = "取消用户授权项目", businessType = BusinessType.GRANT)
//    @RequestPermissions("system:project:add")
//    @PostMapping("/saveProjectRole")
//    public Result saveProjectRole(String sysCode, String roleIds) {
//        List<Long> list = ToolUtils.setIdsToList(roleIds);
//        return sysProjectService.saveProjectRole(sysCode, list);
//    }
//
//    @Operation(summary = "批量取消用户授权项目")
//    @Parameters({
//            @Parameter(name = "sysCode", description = "项目编号"),
//            @Parameter(name = "userIds", description = "用户ID[多个用,隔开]")
//    })
//    @Logs(title = "批量取消授权项目角色", businessType = BusinessType.GRANT)
//    @RequestPermissions("system:project:edit")
//    @PostMapping("/allUserProject")
//    public Result allUserProject(String sysCode, String userIds) {
//        List<Long> list = ToolUtils.setIdsToList(userIds);
//        return toResult(sysProjectService.deleteUserProjectList(sysCode, list));
//    }

}
